package com.itheima.sfbx.insurance.handler.impl;

import com.itheima.sfbx.framework.commons.constant.insure.InsureConstant;
import com.itheima.sfbx.insurance.dto.CustomerRelationVO;
import com.itheima.sfbx.insurance.dto.DoInsureVo;
import com.itheima.sfbx.insurance.handler.InsureHandler;
import com.itheima.sfbx.insurance.service.ICustomerRelationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @ClassName AccidentInsureHandler.java
 * @Description 保障型意外类：保险投保、创建订单、保费计算
 */
@Service
public class AccidentInsureHandler extends SafeguardInsureHandler implements InsureHandler {

    @Resource
    private ICustomerRelationService customerRelationService;

    @Override
    public String doPremium(DoInsureVo doInsureVo) {
        //1、查询被投保人信息  前端传了customerRelationIds里面有被投保人id
        CustomerRelationVO customerRelationVO = customerRelationService.findById(doInsureVo.getCustomerRelationIds().get(0));
        if (customerRelationVO == null) {
            throw new RuntimeException("被投保人不存在!");
        }
        //2、如果被投保人月收入为空或者大于100w的话则抛出异常
        if(customerRelationVO.getIncome() == null ||
                customerRelationVO.getIncome().compareTo(new BigDecimal(1000000.0)) > 0){
            throw new RuntimeException("被投保人月收入不能为空或者大于100w!");
        }
        //3、其它情况继续试算
        return super.doPremium(doInsureVo);
    }
}
